package org.fyp.synchrona.communication;



import java.util.Iterator;
import java.util.List;

import javax.xml.stream.XMLStreamException;

import org.fyp.synchrona.core.impl.Profiler;
import org.fyp.synchrona.core.impl.ServiceManager;
import org.fyp.synchrona.core.impl.ServiceObjCreator;
import org.fyp.synchrona.core.info.impl.CommonProfileObject;
import org.fyp.synchrona.services.utill.FacebookObj;
import org.fyp.synchrona.services.utill.MoodleObj;
import org.fyp.synchrona.services.utill.TwitterObj;
import org.fyp.synchrona.util.DataQueue;
import org.fyp.synchrona.util.PacketIn;
import org.fyp.synchrona.util.Task;
import org.fyp.synchrona.xml.impl.DMMLParser;

public class RequestHandler extends Thread {
	
	private static RequestHandler reqHandler;
	
	private static boolean isRunning ;
	
	private static boolean isInterepted;
	
	private RequestHandler(){
		
	}
	
	public void run(){
		
		while(RequestHandler.getRequestHandler().isRunning){
			int size = DataQueue.getDataQueueObject().getQueueSize();
			System.out.println("Size of the Queue ::"+size);
			try {
				PacketIn inData = DataQueue.getDataQueueObject().dequeue();
				String userName = inData.getUserName();
				String dmml = inData.getBody();
				//for(int i=0;i<100000;i++){
					
					System.out.println(" [INFO][INDATA][USERNAME] ::"+userName);
					System.out.println(" [INFO][INDATA][BODY] ::"+dmml);
					
				//}
				DMMLParser dmmlParser = DMMLParser.getDMMLParser();
				Profiler p = Profiler.getProfilerObject();
				ServiceManager sm = ServiceManager.getServiceMangerObject();
				List taskList = dmmlParser.parseXML(dmml);
				Iterator i = taskList.listIterator();
				Task element = null;
				while(i.hasNext()) {
			
					element = (Task) i.next(); 
					System.out.print(element.getAttributeName()+"\n");		
					
				} 
				
				CommonProfileObject cpo = p.createCommonObject(taskList);
				/*this set of code for testing purpose*/
				System.out.println("testing common profile object ::"+cpo.getUSER_NAME());
				System.out.println("testing common profile object ::"+cpo.getABOUT_ME());
				System.out.println("testing common profile object ::"+cpo.getBIRTH_DAY());
				System.out.println("testing common profile object ::"+cpo.getBIRTH_MONTH());
				System.out.println("testing common profile object ::"+cpo.getBIRTH_YEAR());
				System.out.println("testing common profile object ::"+cpo.getCITY());
				System.out.println("testing common profile object ::"+cpo.getCOUNTRY());
				System.out.println("testing common profile object ::"+cpo.getFIRST_NAME());
				System.out.println("testing common profile object ::"+cpo.getLAST_NAME());
				System.out.println("testing common profile object ::"+cpo.getFULL_NAME());
				System.out.println("testing common profile object ::"+cpo.getUSER_NAME());
				System.out.println("testing common profile object ::"+cpo.getSEX());
				System.out.println("testing common profile object ::"+cpo.getSTATE());
				System.out.println("testing common profile object ::"+cpo.getUSER_NAME());
				System.out.println("testing common profile object ::"+cpo.getADDRESS());
				System.out.println("testing common profile object ::"+cpo.getBIO());
				System.out.println("testing common profile object ::"+cpo.getCOUNTRY_CITY());
				System.out.println("testing common profile object ::"+cpo.getCURRENT_COUNTRY_CITY());
				System.out.println("testing common profile object ::"+cpo.getHOME_PHONE());
				System.out.println("testing common profile object ::"+cpo.getMOBLIE());
				System.out.println("testing common profile object ::"+cpo.getPHOTO_HEIGHT());
				System.out.println("testing common profile object ::"+cpo.getPHOTO_URL());
				System.out.println("testing common profile object ::"+cpo.getPHOTO_WIDTH());
				System.out.println("testing common profile object ::"+cpo.getWEBSITE());
				System.out.println("testing common profile object ::"+element.getSiteName());
				sm.updateServiceProfiles(cpo, element.getSiteName());
				
				/**
				 * Harini's Rules comes here
				 */
				
				
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (XMLStreamException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	/*initialization of request handler*/
	public static synchronized RequestHandler getRequestHandler(){
		if(reqHandler == null){
			reqHandler = new RequestHandler();
			return reqHandler;
		}
		return reqHandler;
	}
	
	/*return running status*/
	public static boolean returnRunningStatus(){
		return RequestHandler.isRunning;
	}
	
	/*set running status*/
	public static void setRunningStatus(boolean status){
		RequestHandler.isRunning = status;
		
	}
	
	/*this method is used to get the interrpt status*/
	public static boolean getInterruptStatus(){
		return RequestHandler.isInterepted;
	}
	
	/*this method is used to set the interrupt status*/
	public static  void setInterruptStatus(boolean interuptStatus){
		RequestHandler.isInterepted = interuptStatus;
	}
	/*this method is uesed to interrupt this thered*/
	public static final void interruptThread() {
		RequestHandler.getRequestHandler().isRunning = false;
		RequestHandler.isInterepted=true;
		RequestHandler.getRequestHandler().suspend();
		
		
	}
	
	public static final void restartThread(){
		
		RequestHandler.getRequestHandler().resume();
		RequestHandler.getRequestHandler().isRunning = true;
		RequestHandler.isInterepted=false;
		
	}
	
	
	
	

}
